#ifndef H_SD
#define H_SD

/* type defines */
typedef struct __attribute__ ((packed)){ /* structure to be passed to sdPostData */
  char powerData[12];  /* 0-11 */
  char temperature[2]; /* 12-13 */
  char time[2];        /* 14-15 */
  char ADCSData[6];    /* 16-21 */
  char payloadData[2]; /* 22-23 */
  char payloadEventID; /* 24 */
  char hash[2];        /* 25-26 */ /* this will be filled in by sdPostData */
  char __padding[5];   /* 27-31 */ /* should always be left blank */
}_sd_record;

union __attribute__ ((packed)) __attribute((aligned(4))){
  unsigned long l;
  unsigned char b[4]; /* top is [2], mid is [1], btm is [0] - functions need to check that [3] is always 0 */
}sdvarlog,sdvarimg;

#define SDHashLocation 25 /* location in bytes of the record hash */
#define SDRecordSize 32 /* size in bytes of record */
#define SDMaxRecord 15 /* maximum number of sd records per sector */

typedef struct __attribute__ ((packed)){
  char record_number;
  _sd_record sd_record[15];
  char __padding[31];
}_sd_sector;

union __attribute__ ((packed)){
  unsigned char r[512];
  _sd_sector s;
}sd_buffer;

/* external functions */
int sdInit(void);
void sdDisable(void);
void sdEnable(void);

int sd_set_blocklen(unsigned long int length);
#define sd_read_block(a) sd_read_block_complex(a,0,512)
int sd_read_block_complex(unsigned long int blockaddr,unsigned int offset,unsigned int size);
int sd_write_block(unsigned long int blockaddr);
void sd_wait_notbusy(void);


#endif /* H_SD */
